package com.dj.mingkong.common.util;

import com.dj.mingkong.common.exception.BizException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public enum JacksonUtil {
    ;
    public static final Logger log = LoggerFactory.getLogger(JacksonUtil.class);
    private static ObjectMapper objectMapper = new ObjectMapper();

    private JacksonUtil() {
    }

    public static <T> T readValue(String jsonStr, Class<T> valueType) {
        if (valueType != null && !StringUtils.isEmpty(jsonStr)) {
            try {
                return objectMapper.readValue(jsonStr, valueType);
            } catch (JsonProcessingException var3) {
                log.error("json 解析异常。 jsonStr: {}, valueType: {}", new Object[]{jsonStr, valueType.getName(), var3});
                throw new BizException("json 解析异常!");
            }
        } else {
            return null;
        }
    }

    public static <T> List<T> jsonToList(String json, Class<T> bean) {
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, new Class[]{bean});

        try {
            return (List) objectMapper.readValue(json, javaType);
        } catch (IOException var4) {
            log.error("jsonToList error ", var4);
            return null;
        }
    }

    public static <T> T readValue(String jsonStr, TypeReference<T> valueTypeRef) {
        if (valueTypeRef != null && !StringUtils.isEmpty(jsonStr)) {
            try {
                return objectMapper.readValue(jsonStr, valueTypeRef);
            } catch (JsonProcessingException var3) {
                log.error("json 解析异常。 jsonStr: {}, valueType: {}", new Object[]{jsonStr, valueTypeRef.getType().getTypeName(), var3});
                throw new BizException("json 解析异常!");
            }
        } else {
            return null;
        }
    }

    public static <T> T readValue(String jsonStr, Class<?> parametrized, Class<?>... parameterClasses) {
        if (parametrized != null && !StringUtils.isEmpty(jsonStr)) {
            JavaType type = objectMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);

            try {
                return objectMapper.readValue(jsonStr, type);
            } catch (JsonProcessingException var5) {
                log.error("json 解析异常。 jsonStr: {}, valueType: {}", new Object[]{jsonStr, type.getTypeName(), var5});
                throw new BizException("json 解析异常!");
            }
        } else {
            return null;
        }
    }

    public static String toJson(Object obj) {
        if (obj == null) {
            return "";
        } else {
            try {
                return objectMapper.writeValueAsString(obj);
            } catch (JsonProcessingException var2) {
                log.error("json 解析异常。 obj: {}", obj, var2);
                throw new BizException("json 解析异常!");
            }
        }
    }

    public static Map<String, Object> toMap(String jsonStr) {
        try {
            return (Map) objectMapper.readValue(jsonStr, new TypeReference<Object>() {
                public Type getType() {
                    return super.getType();
                }
            });
        } catch (JsonProcessingException var2) {
            log.error("json 解析异常。 jsonStr: {}", jsonStr, var2);
            throw new BizException("json 解析异常!");
        }
    }

    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
